package domain;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import util.HibernateUtil;

public class DAOContactGroup extends HibernateDaoSupport implements IDAOContactGroup {


	@Override
	public ContactGroup addContactGroup(final String groupName) {
		/*Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();*/
		return (ContactGroup)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws
			HibernateException{
				ContactGroup group = new ContactGroup();
				group.setGroupName(groupName);
				
				//Checkk if the group doesn't already exist
				Criteria criteria = session.createCriteria(ContactGroup.class).add(Example.create(group));
				List<?> list =  criteria.list();
				if(list.size() == 0) {
					session.save(group);
				}else {
					return null;
				}
				
				return group;
			}
		});

		//getHibernateTemplate().save(group);
		/*session.save(group);
		transaction.commit();
		session.close();*/

	}

	@Override
	public Boolean modifyContactGroup(final long id,final String groupName) {
		/*Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();*/
		return (Boolean)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws
			HibernateException{
				ContactGroup group = (ContactGroup) session.get(ContactGroup.class, id);
				group.setGroupName(groupName);
				session.update(group);
				return true;
			}
		});

		//getHibernateTemplate().save(group);
		/*session.save(group);
		transaction.commit();
		session.close();*/

	}


	@Override
	public boolean deleteContactGroup(final long id) {
		/*Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();*/
		return (Boolean)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws
			HibernateException{
				ContactGroup group = new ContactGroup();
				group.setGroupId(id);
				session.delete(group);
				return true;
			}
		});
		//this.getHibernateTemplate().delete(group);
		/*session.delete(group);
		transaction.commit();*/
	}

	@Override
	public ContactGroup getContactGroup(final long id) {
		return (ContactGroup)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws
			HibernateException{
				ContactGroup group = (ContactGroup) session.get(ContactGroup.class, id);
				//Load the contacts as well as the group
				group.getContacts().size();
				return group;
			}
		});
		/*Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		ContactGroup group = (ContactGroup) session.get(ContactGroup.class, id);*/
		//Charger le liste des contacts en meme temps que les contact
		/*transaction.commit();
		session.close();*/

	}

	@Override
	public Set<ContactGroup> getContactGroups() {
		final Set<ContactGroup> groups= new HashSet<ContactGroup>();
		/*Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();

		Query requete = session.createQuery("select contactgroup from ContactGroup contactgroup");
		List<?> list =  requete.list();*/
		getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws
			HibernateException{

				Query request = session.createQuery("select contactgroup.groupId,contactgroup.groupName from ContactGroup contactgroup" );
				Iterator<?> it = request.iterate();
				while( it.hasNext()){
					Object[] res = (Object[]) it.next();
					ContactGroup cg = new ContactGroup();
					cg.setGroupId((Long)res[0]);
					cg.setGroupName((String)res[1]);
					groups.add(cg);
				}
				/*List<?> list =  request.list();
				for ( Object obj : list){
					groups.add((ContactGroup)obj);
				}*/
				return groups;

			}
		});
		return groups;
		/*transaction.commit();
		session.close();*/

	}
	
	public Set<Contact> getContacts(final long groupId) {
		/*Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();

		Query requete = session.createQuery("select contactgroup from ContactGroup contactgroup");
		List<?> list =  requete.list();*/
		
		final Set<Contact> contacts = new HashSet<Contact>();
		getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws
			HibernateException{
				ContactGroup group = (ContactGroup) session.get(ContactGroup.class, groupId);
				for ( Contact contact : group.getContacts())
					contacts.add(contact);
				/*List<?> list =  request.list();
				for ( Object obj : list){
					groups.add((ContactGroup)obj);
				}*/
				return contacts;

			}
		});
		return contacts;
		/*transaction.commit();
		session.close();*/

	}

}
